package day07.Test1;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface Speakable {
    void speak(String message);
}

abstract class Person implements Speakable {
    public abstract String toString();
}

class Teacher extends Person {
    public Teacher() {
    }

    public String positon;
    private int salary;

    public void speak(String message) {
        System.out.println("Speak: " + message);
    }

    @Override
    public String toString() {
        return "[Positon=" + positon + " salary= " + salary + "]";
    }

    public int getSalary() {
        return salary;
    }
}

//代理类
class PersonProxy implements InvocationHandler {

    private Object target;

    public PersonProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Class begin");
        //通过反射实现
        Object result = method.invoke(target, args);
        System.out.println("Class end");

        return result;
    }
}


public class TextProxy {
    public static void main(String[] args) {
        Speakable target = new Teacher();
        Speakable result = (Speakable) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),// 真实对象的类加载器
                new Class[]{Speakable.class},// 真实对象实现的接口数组
                new PersonProxy(target));// 代理处理器实例
        result.speak("1、The java.lang and java.lang.reflect packages provide classes for java reflection.\n" +
                "2、Java Reflection is a process of examining or modifying the run time behavior of a class at run time.\n" +
                "3、The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class");
    }
}
